package com.jalarbee.stock.sales.business;

import com.jalarbee.core.business.PricedEntity;
import com.jalarbee.stock.product.business.Product;
import com.jalarbee.stock.product.business.Quantity;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import org.hibernate.annotations.Columns;
import org.hibernate.annotations.Type;

/**
 *
 * @author Abdoulaye <b>Ikhnaton</b> Diallo
 */
@Entity
public class LineItem extends PricedEntity implements Serializable {

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private long id;
    
    @Type(type = "com.jalarbee.core.types.QuantityType")
    @Columns(columns= {
        @Column(name="SIZE", nullable=false),
        @Column(name="UNIT", nullable=false)
    })
    private Quantity quantity;
    
    @ManyToOne
    private Product product;
    
    @ManyToOne
    private Sale sale;

    private LineItem() {
    }

    
    public LineItem(Quantity quantity, Product product) {
        this.quantity = quantity;
        this.product = product;
        fillPrices();
    }
    
    
    private void fillPrices() {
        setRetailPrice(product.getRetailPrice().times(quantity.getCount()));
        setStockPrice(product.getStockPrice().times(quantity.getCount()));
        setCreditPrice(product.getCreditPrice().times(quantity.getCount()));
    }
    
    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public Product getProduct() {
        return product;
    }

    public void setProduct(Product product) {
        this.product = product;
    }

    public Quantity getQuantity() {
        return quantity;
    }

    public void setQuantity(Quantity quantity) {
        this.quantity = quantity;
    }

    public Sale getSale() {
        return sale;
    }

    public void setSale(Sale sale) {
        this.sale = sale;
    }
    
}
